/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.model;

import com.ipcc.manager.entity.IPCC_EXTENSIONS;
import com.ipcc.manager.entity.IPCC_EXTENSION_GROUPS;
import com.ipcc.manager.entity.PARTNER;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class IPCC_EXTENSION_GROUPSModel {
    public static List<IPCC_EXTENSION_GROUPS> getAll(String condition) throws Exception{
        List<IPCC_EXTENSION_GROUPS> list = new ArrayList<IPCC_EXTENSION_GROUPS>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "SELECT * FROM ipcc_extension_groups "
                + " " + condition
                + " ORDER BY GROUP_NAME";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                IPCC_EXTENSION_GROUPS ipg = new IPCC_EXTENSION_GROUPS();
                ipg.setGroup(rs.getString(1));
                ipg.setGroup_name(rs.getString(2));                
                PARTNER p = new PARTNER();
                p.setPartner_id(Integer.parseInt(rs.getString(3)));
                ipg.setPartner_id(p);
                list.add(ipg);
            }
            return list;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }
    public static String getNameById(String id) throws Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " SELECT group_name FROM ipcc_extension_groups "
                + " WHERE group_ = '"+id+"' ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            if (rs.next()) {
                return rs.getString(1);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return "";
    }
    public static String getIdBySource_number(String source_number) throws Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select a.group_ from ipcc_extension_groups a,ipcc_group_channel b"
                + " where a.group_ = b.group_ and b.source_number = '"+source_number+"' ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            if (rs.next()) {
                return rs.getString(1);
            }
            return "";
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }
    
    public static String getIdByName(String name) throws Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " SELECT group_ FROM ipcc_extension_groups "
                + " WHERE group_name = '"+name+"' ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            if (rs.next()) {
                return rs.getString(1);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return "";
    }
}
